clear all
clear matrix
version 13
capture log close
set matsize 11000
set more off, permanently

*-------------------------------------------------------------------------------
* Options	
*-------------------------------------------------------------------------------
global date = "2306"
local year = 2020
global niter = 15

global stub 	 = "/Volumes/Transcend/AWFP/ThirdChapter_revision"
global main		 = "$stub/data"
global inputcali = "$stub/data/CalibrationInput"	
global outputcali= "$stub/data/CalibrationOutput"	
global data_proc = "$stub/data"
global graphs    = "$stub/graphs"	
global tables    = "$stub/tables"
global do        = "$stub/do"
global excel     = "$stub/excel"	
global orig      = "$stub/orig"

cd "$main"
********************************************************************************
import delimited ${outputcali}/param${date}`year'.csv, clear
ren v1 iter
ren v2 alpha
ren v3 psi
ren v4 gamma
save ${outputcali}/param${date}`year'.dta, replace

forv ii = 1(1)$niter {
use "${outputcali}/${date}`year'/tosend_${date}`year'_`ii'.dta", clear
drop aux*
egen id = group(amr eventdate)
reshape long v tot_empl, i(id) j(bin)
		
drop if v==0			/* employment value is equal to 0 */
gen iter = `ii'
merge m:1 iter using ${outputcali}/param${date}`year'.dta
drop if _merge!=3
drop _merge

tab iter

gen mult = psi^(2*bin/(1-alpha))
by amr eventdate, sort: egen D = total(mult*tot_empl)
keep amr eventdate D
duplicates drop

reshape wide D, i(eventdate) j(amr)
gen idtime = _n

keep D*
export delimited "${outputcali}/D_ts_estimation`ii'_${date}.csv", replace novarnames
}
********************************************************************************
